package j.todo;

import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class SBAttachActivity extends Activity {
	private RelativeLayout back;
	private RelativeLayout.LayoutParams params;
	private DBAdapter db;
	private Cursor c;
	private ArrayList<SBItemButton> itemList;
	private SBItemButton item;
	private Button editBtn;
	public static boolean EDIT_MODE = false;
	public static boolean SAVE_MODE = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init();
		setContentView(back);

	}

	@Override
	protected void onResume() {
		super.onResume();
		buildDataFromDB();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_MENU:
			if (editBtn.getVisibility() == View.VISIBLE) {
				editBtn.setVisibility(View.INVISIBLE);
			} else {
				editBtn.setVisibility(View.VISIBLE);
			}
			break;
		case KeyEvent.KEYCODE_BACK:
			save();
			break;
		}
		return super.onKeyDown(keyCode, event);
	}

	public void init() {
		editBtn = new Button(this);
		editBtn.setBackgroundResource(R.drawable.edit);
		editBtn.setVisibility(View.INVISIBLE);
		params = new RelativeLayout.LayoutParams(60, 60);
		params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		params.setMargins(0, 7, 3, 0);
		editBtn.setLayoutParams(params);
		editBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (EDIT_MODE) {
					EDIT_MODE = false;
				} else {
					EDIT_MODE = true;
				}

			}
		});

		back = new RelativeLayout(this);
		back.setBackgroundResource(R.drawable.back);
		params = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.MATCH_PARENT,
				RelativeLayout.LayoutParams.MATCH_PARENT);
		itemList = new ArrayList<SBItemButton>();

		back.addView(editBtn);
	}

	public void buildDataFromDB() {

		// if (!SAVE_MODE) {
		db = new DBAdapter(this);

		db.open();

		c = db.fetchAllMemo();

		while (c.moveToNext()) {
			SBItemButton item = new SBItemButton(this);
			item.setLine(c.getString(1));
			item.setPrior(c.getString(3));
			item.setText(item.getLine());
			double xx = Math.random();
			xx *= 400;
			double yy = Math.random();
			yy *= 700;
			item.setX((int) xx);
			item.setY((int) yy);

			params = new RelativeLayout.LayoutParams(item.getItemWidth(),
					item.getItemHeight());
			params.setMargins(item.getX(), item.getY(), 0, 0);
			item.setLayoutParams(params);
			back.addView(item);
		}
		db.close();
		// }
	}

	public void save() {
		// SAVE_MODE = true;
		// SQLiteOpenHelper helper = new SBDataBaseHelper(this, "setting", null,
		// 1);
		// SQLiteDatabase database = helper.getWritableDatabase();
		// String sql = "select * from "
		// + SBDefineConstant.getInstance().WORKING_SBTABLE_NAME;
		// Cursor c = database.rawQuery(sql, null);
		// c.moveToNext();
		// Toast.makeText(this, "ImageUri: " + c.getString(5), 1000).show();
		// database.close();

	}
}
